package edu.lingnan.lingnanblog.controller;

//import edu.lingnan.lingnanblog.controller.admin.DateJsonValueProcessor;
import edu.lingnan.lingnanblog.entity.Blog;
import edu.lingnan.lingnanblog.entity.BlogType;
import edu.lingnan.lingnanblog.entity.PageBean;
//import edu.lingnan.lingnanblog.service.BlogService;
//import edu.lingnan.lingnanblog.service.BlogTypeService;
import edu.lingnan.lingnanblog.service.BlogClientService;
import edu.lingnan.lingnanblog.utils.*;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
import edu.lingnan.lingnanblog.entity.Blog;
import edu.lingnan.lingnanblog.entity.PageBean;
import edu.lingnan.lingnanblog.utils.ResponseUtil;
import edu.lingnan.lingnanblog.utils.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wuyuan
 * @since 2020-06-15 22:08
 */
@Controller
@RequestMapping("/admin/blog")
public class BlogConsumerController {

    @Autowired
    private BlogClientService blogClientService;

    @RequestMapping("toEditBlog")
    public String toEditBlog(){
        return "admin/blogManage";
    }

    /**
     * 分页查询博客信息
     * @param page
     * @param rows
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/list")
    public String list(@RequestParam(value="page",required=false)String page, @RequestParam(value="rows",required=false)String rows, Blog s_blog, HttpServletResponse response)throws Exception{
        PageBean pageBean=new PageBean(Integer.parseInt(page),Integer.parseInt(rows));
        Map<String,Object> map=new HashMap<String,Object>();
        map.put("title", StringUtil.formatLike(s_blog.getTitle()));
        map.put("start", pageBean.getStart());
        map.put("size", pageBean.getPageSize());
        map.put("blog", s_blog);
        List<Blog> blogList=blogClientService.list(map);
        Long total=blogClientService.getTotal(map);
        JSONObject result=new JSONObject();
        JsonConfig jsonConfig=new JsonConfig();
        jsonConfig.registerJsonValueProcessor(java.util.Date.class, new DateJsonValueProcessor("yyyy-MM-dd"));
        JSONArray jsonArray=JSONArray.fromObject(blogList,jsonConfig);
        result.put("rows", jsonArray);
        result.put("total", total);
        ResponseUtil.write(response, result);
        return null;
    }

    // 博客索引
    private BlogIndex blogIndex=new BlogIndex();
    /**
     * 添加或者修改博客信息
     * @param blog
     * @param response
     * @return
     * @throws Exception
     */

    @RequestMapping("/save")
    public String save(Blog blog, HttpServletResponse response)throws Exception{

        System.out.println("========================");
        System.out.println("blog = " + blog );
        System.out.println("========================");

        blog.setBlogType(null);
        BlogType blogType = blog.getBlogType();
        // 操作的记录条数
        int resultTotal=0;
        if(blog.getId()==null){
            resultTotal=blogClientService.add(blog, blogType);
            // 添加博客索引
            blogIndex.addIndex(blog);
        }else{
            resultTotal=blogClientService.update(blog, blogType);
            // 更新博客索引
            blogIndex.updateIndex(blog);
            System.out.println(blog);
        }
        JSONObject result=new JSONObject();
        if(resultTotal>0){
            result.put("success", true);
            //		session.setAttribute("blogMsg",blog);
        }else{
            result.put("success", false);
        }
        ResponseUtil.write(response, result);
        return null;
    }

    /**
     * 删除博客信息
     * @param ids
     * @param response
     * @return
     * @throws Exception
     */
    @RequestMapping("/delete")
    public String delete(@RequestParam(value="ids")String ids, HttpServletResponse response)throws Exception{
        String []idsStr=ids.split(",");
        for(int i=0;i<idsStr.length;i++){
            blogClientService.delete(Integer.parseInt(idsStr[i]));
            blogIndex.deleteIndex(idsStr[i]); // 删除对应博客的索引
        }
        JSONObject result=new JSONObject();
        result.put("success", true);
        ResponseUtil.write(response, result);
        return null;
    }


    @RequestMapping("/getById")
    public String getById(@RequestParam(value="id")String id, HttpServletResponse response)throws Exception{
        Blog blog=blogClientService.getById(Integer.parseInt(id));
        JSONObject jsonObject=JSONObject.fromObject(blog);
        ResponseUtil.write(response, jsonObject);
        return null;
    }

    @RequestMapping("toWriteBlog")
    public String toWriteBlog(Model model){
        List<BlogType> blogTypeCountList = blogClientService.toWriteBlog();
        model.addAttribute("blogTypeCountList", blogTypeCountList);
        return "admin/writeBlog";
    }

    @RequestMapping("toModifyBlog")
    public String toModifyBlog(@RequestParam("id") String id, Model model){
        List<BlogType> blogTypeCountList = blogClientService.toModifyBlog();
        model.addAttribute("blogTypeCountList", blogTypeCountList);
        model.addAttribute("id",id);
        return "admin/modifyBlog";
    }
}
